package org.cqust.backend.service.mission;

import lombok.RequiredArgsConstructor;
import org.cqust.backend.exception.BusinessException;
import org.cqust.backend.dto.request.mission.attempt.MissionStepAttemptRequestDTO;
import org.cqust.backend.dto.response.mission.attempt.MissionAttemptCreateDTO;
import org.cqust.backend.entity.mission.MissionVersion;
import org.cqust.backend.entity.mission.attempt.MissionAttempt;
import org.cqust.backend.entity.mission.attempt.MissionStepAttempt;
import org.cqust.backend.repository.mission.MissionAttemptRepository;
import org.cqust.backend.repository.mission.MissionStepAttemptRepository;
import org.cqust.backend.repository.mission.MissionVersionRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class MissionAttemptService {
    private final MissionStepAttemptRepository missionStepAttemptRepository;
    private final MissionAttemptRepository missionAttemptRepository;
    private final MissionVersionRepository missionVersionRepository;

    @Transactional
    public MissionAttemptCreateDTO createAttempt(Long missionId, Long userId){
        MissionVersion missionVersion = missionVersionRepository.findCurrentMissionVersionByMissionId(missionId)
                .orElseThrow(() -> new BusinessException("未找到ID为 " + missionId + " 的任务"));
        MissionAttempt newAttempt = new MissionAttempt();
        newAttempt.setMissionVersion(missionVersion);
        newAttempt.setUserId(userId);
        newAttempt.setStartTime(LocalDateTime.now());
        newAttempt.setEndTime(LocalDateTime.now());
        newAttempt.setScore(0f);
        missionAttemptRepository.save(newAttempt);
        MissionAttemptCreateDTO responseDTO = new MissionAttemptCreateDTO();
        responseDTO.missionAttemptId = newAttempt.getId();
        return responseDTO;
    }

    @Transactional
    public boolean recordStepAttempt(MissionStepAttemptRequestDTO attemptRequest, Long userId) {
        MissionAttempt missionAttempt = missionAttemptRepository.findById(attemptRequest.getMissionAttemptId())
                .orElseThrow(() -> new BusinessException("未找到ID为 " + attemptRequest.getMissionAttemptId() + " 的任务记录"));

        if(!Objects.equals(missionAttempt.getUserId(), userId))
            throw new BusinessException("任务记录所属用户不匹配");

        MissionStepAttempt stepAttempt = new MissionStepAttempt();
        stepAttempt.setId(null);
        stepAttempt.setMissionAttemptId(missionAttempt.getId());
        stepAttempt.setStepVersionId(attemptRequest.getStepVersionId());
        stepAttempt.setScore(missionAttempt.getScore());

        stepAttempt = missionStepAttemptRepository.save(stepAttempt);

        //将分数追加到步骤所属的任务记录中。
        missionAttempt.setScore(missionAttempt.getScore() + attemptRequest.getScore());
        missionAttempt.setEndTime(stepAttempt.getCreatedAt());
        missionAttemptRepository.save(missionAttempt);
        return true;
    }
}
